<?php

namespace App\Repository\Information;

use App\Models\Information\AppPageConfModel as model;
use Illuminate\Support\Facades\Cache;
use App\Repository\RoleRepository;
use Illuminate\Support\Facades\DB;
use App\Repository\BaseRepository;
use App\Repository\Product\ProductRepository;
use App\Repository\UploadRepository;
use App\Repository\Product\GoodsRemarkRepository;

class AppPageConfRepository extends  BaseRepository
{
  private $model;
  private $productModel;
  public function __construct()
  {
    $this->model = new model();
    $this->productModel = new ProductRepository();
    parent::__construct();
  }
  //列表
  public function index($where)
  {
    $cron = $where;
    if (empty($cron['order'])) {
      // $cron['order'] = ['orderby' => 'id', 'orderdir' => 'desc'];
      $cron['order'] = ['orderby' => ['id' => 'desc']];
    }

    // $cron['where'] = $where;
    // var_dump($cron, 111111111);

    $data = $this->search($this->model, $cron, 1, 9000000);
    if (!empty($data['data'])) {
      $data['data'] = usfirstArr($data['data']);
    }
    return $data;
  }
  //添加
  public function add($data)
  {
    //必填 字段
    $mustField = [
      'name',
      'goods_id'
    ];
    $optionalFields = $this->model->getTableField();
    $msgField = [
      'name' => '商品名称不能为空',
      'goods_id' => '请选择商品或存量'
    ];
    $insertData  = $this->getRealData($data, $mustField, $optionalFields, $msgField);
    if (!empty($insertData) && is_string($insertData)) {
      //返回错误参数
      return $this->returnMsg(
        2,
        $insertData
      );
    }
    $isTrue = $this->getOne(['name' => $insertData['name'], 'type' => $insertData['type'], 'shop_id' => $data->user->shop_id]);
    if ($isTrue) {
      return $this->returnMsg(3, '该页面配置已添加');
    }
    $insertData['created_at'] = date('Y-m-d H:i:s');
    $insertData['created_at'] = date('Y-m-d H:i:s');
    $insertData['created_by'] = $data->user->id;
    $insertData['created_by_name'] = $data->user->user_name;
    $insertData['shop_id'] = (!empty($data->user->shop_id) ? $data->user->shop_id : (!empty($data->user->id) ? $data->user->id : ''));



    $res = $this->model->insertGetId($insertData);

    if ($res) {
      return $this->returnMsg(200, '添加成功');
    }
    return $this->returnMsg(1, '添加失败');
  }
  //删除
  public function del($where)
  {
    $flag = true;
    $msg = '未知错误';
    $code = 1;
    if (empty($where)) {
      $flag = false;
      $msg = '删除条件不能为空';
      $code = 2;
    }
    if ($flag) {
      $res = $this->model->where($where)->get();
      if (!$res) {
        $flag = false;
        $msg = '未查询到数据';
        $code = 3;
      }
    }
    if ($flag) {
      $del = $this->model->where($where)->delete();
      if ($del) {
        $msg = '删除成功';
        $code = 200;
      }
    }
    return $this->returnMsg($code, $msg);
  }
  //修改
  public function edit($where, $data, $isMust = false)
  {
    if (empty($where)) {
      return $this->returnMsg(1, '条件不能为空');
    }
    if (empty($data)) {
      return $this->returnMsg(2, '修改参数不能为空');
    }
    //过滤字段
    // var_dump($data->all());
    //过滤字段
    $optionalFields = $this->model->getTableField();
    $field  = $this->getRealData($data, [], $optionalFields, []);
    // if (!empty($field['main_pic']) && is_string($field['main_pic']) && file_exists('.' . $field['main_pic'])) {
    //   unset($field['main_pic']);
    // }
    if (!empty($field['main_pic']) && strlen($field['main_pic']) > 1000) {
      $model = new UploadRepository();
      $resPic = $model->uploadBase64($data['main_pic']);
      // var_dump($resPic);
      if (empty($resPic)) {
        return [
          'code' => 100,
          'msg' => '上传失败',
          'fileName' => ''
        ];
      }
      $field['main_pic'] = $resPic;
    }
    if (!empty($field['detail']) && strlen($field['detail']) > 10000) {
      return $this->returnMsg(104, '上传数据太长,请检测上传的图片,请尽量使用上面图片上传按钮');
    }
    if (empty($field['main_pic'])) {
      unset($field['main_pic']);
    }
    //查询是否存在
    $isTrue = $this->model->where($where)->first();
    if (!$isTrue) {
      return $this->returnMsg(3, '未查询到配置');
    }
    // var_dump($field, 1111111111);
    //先查询是否存在
    $res = $this->model->where($where)->update($field);

    //先查询是否存在
    // $res = $this->model->where($where)->update($field);
    if ($isMust && $res) {
      return $this->returnMsg(200, '修改成功');
    }
    if (!$isMust) {
      return $this->returnMsg(200, '修改成功');
    }
    return $this->returnMsg(3, '请重新修改');
  }
  //列表查询
  public function lists($where = [], $page = 1, $limit = 10)
  {
    $res = $this->search($this->model, $where, $page, $limit);

    return $res;
  }
  public function getOne($where, $field = ['*'])
  {
    return $this->model->where($where)->select($field)->first();
  }
}
